# coding:utf-8
__author__ = "zhou"
# create by zhou on 2021/5/20
from django.contrib.gis.db import models
import time
import uuid
from . import manager


class AlgoInfo(models.Model):
    # 算法表
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, unique=True, default='')
    event_id = models.CharField(max_length=255, unique=True, default='')
    algo_module_name = models.CharField(max_length=500, default='')
    addtime = models.PositiveIntegerField(default=0)
    updatetime = models.PositiveIntegerField(default=0)
    online = models.PositiveIntegerField(default=0)     # 0 未上线  1已上线
    onlinetime = models.PositiveIntegerField(default=0)
    risk_level = models.PositiveIntegerField(default=0) # 风险等级

    objects = manager.MyManager(using='smart_community')
    class Meta:
        db_table = 'algo_info'
        verbose_name = '算法表'


    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        # 自动更新 updatetime 的值
        self.updatetime = int(time.time())
        models.Model.save(self, force_insert, force_update, using, update_fields)



class CameraInfo(models.Model):
    # 摄像机表
    id = models.AutoField(primary_key=True)
    cam_id = models.CharField(max_length=255, unique=True, default='')
    rtsp_url = models.CharField(max_length=255, unique=True, default='') # 流地址
    name = models.CharField(max_length=255, unique=True, default='')
    gis = models.GeometryField(null=True, blank=True)
    addtime = models.PositiveIntegerField(default=lambda:int(time.time()))
    updatetime = models.PositiveIntegerField(default=lambda:int(time.time()))

    objects = manager.MyManager(using='smart_community')
    class Meta:
        db_table = 'camera_info'
        verbose_name = '摄像机表'


class CameraAlgoConfig(models.Model):
    # 摄像机算法配置表
    id = models.AutoField(primary_key=True)
    cam_id = models.CharField(max_length=255, default='')
    event_id = models.CharField(max_length=255, default='')
    area_info = models.TextField()
    addtime = models.PositiveIntegerField(default=lambda:int(time.time()))
    updatetime = models.PositiveIntegerField(default=0)

    objects = manager.MyManager(using='smart_community')
    class Meta:
        db_table = 'camera_algo_config'
        verbose_name = '摄像机算法配置表'
        unique_together = [('cam_id', 'event_id')] # 符合唯一索引


    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        # 自动更新 updatetime 的值
        self.updatetime = int(time.time())
        models.Model.save(self, force_insert, force_update, using, update_fields)


class ImageInfo(models.Model):
    id = models.AutoField(primary_key=True)
    b64_content = models.TextField()
    b64_md5 = models.CharField(max_length=32, default='')
    addtime = models.PositiveIntegerField(default=lambda:int(time.time()))

    objects = manager.MyManager(using='smart_community')
    class Meta:
        db_table = 'image_info'
        verbose_name = '图片表'


class CameraAlgoAlertLog(models.Model):
    # 摄像机告警表
    id = models.AutoField(primary_key=True) # 主键
    log_id = models.CharField(max_length=32, unique=True,default=lambda:uuid.uuid1().hex
                              )  # 辅助唯一键，前端可能有用  uuid1 hex
    cam_id = models.CharField(max_length=255, db_index=True)
    event_id = models.CharField(max_length=255, db_index=True)
    image_b64_md5 = models.CharField(max_length=32, default='')
    image_mark_b64_md5 = models.CharField(max_length=32, default='')
    addtime = models.PositiveIntegerField(default=0)
    meta = models.TextField(default='')

    objects = manager.MyManager(using='smart_community')
    class Meta:
        db_table = 'camera_algo_alert_log'
        ordering = ["-id"]
        verbose_name = '摄像机算法告警表'